
minetest.register_privilege("levels_admin", {"Grants Access to Player Level Editor Commmands", give_to_singleplayer = false})

-- Sets a Level to a player
minetest.register_chatcommand("set_level",{
  description = "Sets a specified level to a player.",
  params = "<playername> <level>",
  func = function(name, param)
    local target = (param:split(' ')[1])
    local level = tonumber((param:split(' ')[2]))
    local verified = nil

    if not minetest.check_player_privs(name, {levels_admin=true}) then

      return false, "**You are not authorized to change or set someone\'s player level!"
    end

    if minetest.check_player_privs(name, {levels_admin=true}) then


      if target == "" or target == nil  then

        minetest.chat_send_player(name, "*You must add a player name to execute this command!")
 
      else


        if minetest.env:get_player_by_name(target) == "" or minetest.env:get_player_by_name(target) == nil then
          minetest.chat_send_player(name, "*Invalid player name!")

        else

          local playername = nil

          for _,player in ipairs(minetest.get_connected_players()) do
            local name = player:get_player_name()

            if name == target then
              playername = name
              verified = player
              break
            end

          end

          if level == "" or level == nil then

            minetest.chat_send_player(name, "*You must include a number for "..playername.."\'s player level!")

          else

            if level >=1  then

              set_player_level(verified, level)
              check_player_current_level(verified)
              minetest.chat_send_player(name, "*Successfully changed "..playername.."\'s player level to "..level.."!")

            else

              minetest.chat_send_player(name, "*Player level must be 1 or greater!")
  
            end

          end

        end

      end
  
    end

  end
});

-- Sets a number of EXP to a player 
minetest.register_chatcommand("set_exp",{
  description = "Sets total EXP to player.",
  params = "<playername> <level>",
  func = function(name, param)
  local target = (param:split(' ')[1])
  local exp = tonumber((param:split(' ')[2]))
  local verified = nil

    
    if not minetest.check_player_privs(name, {levels_admin=true}) then

      return false, "**You are not authorized to change or set someone\'s total EXP!"
    end


    if minetest.check_player_privs(name, {levels_admin=true}) then


      if target == "" or target == nil  then

        minetest.chat_send_player(name, "**You must add a player name to execute this command!")

      else


        if minetest.env:get_player_by_name(target) == "" or minetest.env:get_player_by_name(target) == nil then
          minetest.chat_send_player(name, "**Invalid player name!")

        else

          local playername = nil

          for _,player in ipairs(minetest.get_connected_players()) do
            local name = player:get_player_name()

            if name == target then
              playername = name
              verified = player
              break
            end

          end

          if exp == "" or exp == nil then

            minetest.chat_send_player(name, "**You must include a number for "..playername.."\'s EXP!")

          else


            if exp >= 0 then
              local required_exp = tonumber(get_player_required_exp(verified))

              --Avoid overflow
              if exp >= required_exp then
                exp = required_exp
              end

              minetest.chat_send_player(name, "**Successfully changed "..playername.."\'s EXP to "..exp.."!")
 
              set_player_exp(verified, exp)
              check_player_current_level(verified)

            else

              minetest.chat_send_player(name, "**EXP must be 0 or greater!")
  
            end

          end

        end

      end

    end

  end
});

-- Sets a number of EXP to a player 
minetest.register_chatcommand("add_exp",{
  description = "Adds a specified ammount of EXP to a player.",
  params = "<playername> <exp>",
  func = function(name, param)
  local target = (param:split(' ')[1])
  local exp = tonumber((param:split(' ')[2]))
  local verified = nil

    
    if not minetest.check_player_privs(name, {levels_admin=true}) then

      return false, "**You are not authorized to change or set someone\'s total EXP!"
    end


    if minetest.check_player_privs(name, {levels_admin=true}) then


      if target == "" or target == nil  then

        minetest.chat_send_player(name, "**You must add a player name to execute this command!")

      else


        if minetest.env:get_player_by_name(target) == "" or minetest.env:get_player_by_name(target) == nil then
          minetest.chat_send_player(name, "**Invalid player name!")

        else

          local playername = nil

          for _,player in ipairs(minetest.get_connected_players()) do
            local name = player:get_player_name()

            if name == target then
              playername = name
              verified = player
              break
            end

          end

          if exp == "" or exp == nil then

            minetest.chat_send_player(name, "**You must include a number for "..playername.."\'s EXP!")

          else


            if exp >= 0 then
              local required_exp = tonumber(get_player_required_exp(verified))

              --Avoid overflow
              if exp >= required_exp then
                exp = required_exp
              end

              minetest.chat_send_player(name, "**Successfully added "..exp.." EXP to "..playername.."!")
 
              add_player_exp(verified, exp)

              check_player_current_level(verified)

            else

              minetest.chat_send_player(name, "**EXP must be 0 or greater!")
  
            end

          end

        end

      end

    end

  end
});

-- Sets Level Cap
minetest.register_chatcommand("set_level_cap", {
  params = "<level>",
  description = "Sets or changes the max level cap.",
  func = function(name, param)
    local level = tonumber(param)
    local players = minetest.get_connected_players()


    if not minetest.check_player_privs(name, {server=true}) then
      return false, "**You are not authorized to change or set the max level cap!"
    end

    if type(level) == "number" then

      if level >= 1 then

        set_level_cap(level)

        --update everyone's level status
        for k,v in ipairs(players) do
          local player = minetest.get_player_by_name(v)
          check_player_current_level(player)
        end

        minetest.chat_send_player(name, "**Player level cap is now set to "..level.."!")

      else

        return false, "**Level cap must be greater than 0!"

      end

    else

      return false, "**You must insert a number!"

    end

  end
});
